package org.ccunix.eshop.service.impl;

import org.ccunix.eshop.dao.ShopCartDao;
import org.ccunix.eshop.domain.dto.CartSelectedMerVO;
import org.ccunix.eshop.domain.dto.CartVO;
import org.ccunix.eshop.domain.dto.MerchandiseVO;
import org.ccunix.eshop.domain.dto.OrdersVO;
import org.ccunix.eshop.service.MerchandiseServiceIface;
import org.ccunix.eshop.service.OrdersServiceIface;
import org.ccunix.eshop.service.ShopCarServiceIface;
import org.ccunix.eshop.utils.DataFormatUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class ShopCarServiceImpl implements ShopCarServiceIface {
    ShopCartDao shopCartDao = new ShopCartDao();
    MerchandiseServiceIface merchandiseService = new MerchandiseServiceImpl();
    OrdersServiceIface ordersService = new OrdersServiceImpl();
    @Override
    public CartVO getShopCart(Integer member) {
        //先从数据库中查找是否存在车
        CartVO cartVO = shopCartDao.selectCartByMember(member);
        if (cartVO==null){
            //创建一个
            shopCartDao.insert(member);
            cartVO = shopCartDao.selectCartByMember(member);
        }
        return cartVO;
    }

    @Override
    public List<CartSelectedMerVO> getCartSelectedMerListByCar(Integer cart) {
        return shopCartDao.selectCartSelectMerListByCar(cart);
    }

    @Override
    public boolean addShopCar(Integer cart, String merId ,Integer favourable) {
        //获得商品信息
        MerchandiseVO merchandiseVO = merchandiseService.getMerchantById(merId);
        boolean b = false;
        //去实现
        //该商品是否存在在购物车内  存在 cart merchandise 1 更新
        CartSelectedMerVO cartSelectedMerVO = getCartSelectedMerByCarAndMid(cart,merId);
        //不存在 那么 进行插入
        if (cartSelectedMerVO==null){
            //insert
            //商品是否是特价 那么存什么 特价和会员价比较 要最小值
            BigDecimal calcPrice = null;
            //打折后的
            BigDecimal favourablePrice = merchandiseVO.getPrice().multiply(new BigDecimal(String.valueOf(favourable)));
            favourablePrice = favourablePrice.multiply(new BigDecimal("0.01"));
            if(merchandiseVO.getSpecial()==1){
                //特价
                BigDecimal special = merchandiseVO.getSprice();

                //比较BigDecimal
                if (special.compareTo(favourablePrice)==1){
                    System.out.println("special大于favourablePrice(特价大于会员价)");
                    calcPrice = favourablePrice;
                }else {
                    calcPrice = special;
                }
            }else{
                //商品是新品 那么按照会员价格
                calcPrice = favourablePrice;
            }
            //插入数据库
            b = addCartSelectedMer(cart,merchandiseVO.getId(),calcPrice);

        }else {
            //update
            b = updateCartSelectedMerNumber(cartSelectedMerVO.getId(),cart);//更新购物项 更新购物车总金额
        }
        return b;
    }

    @Override
    public boolean updateCartSelectedMerNumber(Integer id,Integer cart) {
        //更新商品项

        //更新购物车总金额
        return shopCartDao.updateCartSelectedMerNumber(id,cart);
    }

    @Override
    public CartSelectedMerVO getCartSelectedMerByCarAndMid(Integer cart, String merId) {
        /**
         * 查询
         */
        int merchandise = Integer.parseInt(merId);
        return shopCartDao.selectCartSelectedMerByCarAndMid(cart,merchandise);
    }

    @Override
    public boolean addCartSelectedMer(Integer cart, Integer merchandise, BigDecimal price) {
        return shopCartDao.addCartSelectedMer(cart,merchandise,price);
    }

    @Override
    public boolean deleteCartSelectedMerById(String id,int cart) {
        int int_id = Integer.parseInt(id);
        return shopCartDao.deleteCartSelectedMerById(int_id,cart);
    }

    @Override
    public boolean clearCartSelectedMerByCart(Integer cart) {
        return shopCartDao.deleteCartSelectedMerByCart(cart);
    }

    @Override
    public boolean updateCartSelectedMerNumById(String id,int num,int cart) {
        int int_id = Integer.parseInt(id);
        return shopCartDao.updateCartSelectedMerNumById(int_id,num,cart);
    }

    @Override
    public OrdersVO submitShopCar(Integer cart, Integer member) {
        String orderDate = DataFormatUtil.formatDateString(new Date(),"yyyy-MM-dd HH:mm:ss");
        String orderNo = DataFormatUtil.getOrderNoByUUIDHashCode("");
        //提交购物车
        boolean b = shopCartDao.submitShopCar(cart,member,orderNo,orderDate);
        if (b){
            return ordersService.getOrderByOrderNo(orderNo);
        }
        return null;
    }
}
